/*
    PDA端提交回的库存数是最小单位，需要转化成箱；
    零售价也是最小单位，需要转换成箱的价格
    W.P.  2010-8-6
*/
trigger TinBoxToCase on Inventory__c (before insert) 
{
    if (trigger.new.size() > 0)
    {
        string soql = 'Select Product_Package__c, Pack_Size_C__c From Product2 where id in (';
        for (Inventory__c inventory : trigger.new)
        {
                soql += '\'' + inventory.Product_Name__c + '\',';
        }
        soql = soql.substring(0, soql.length() - 1) + ')';
        
        List<Product2> products = Database.query(soql);
        if (products.size() > 0)
        {
            for (Inventory__c inventory : trigger.new)
            {
                for(Product2 product : products)
                {
                    if(inventory.Product_Name__c == product.Id)
                    {
                        //if(product.Product_Package__c == 'Case')
                        //{
                        try
                        {
                            inventory.InventoryToCase__c = inventory.Inventory__c / product.Pack_Size_C__c;
                            inventory.RetailPriceToCase__c = inventory.Retail_Price__c * product.Pack_Size_C__c;
                        }
                        catch(NullPointerException ex)
                        {
                            inventory.addError('库存和零售价不能为空！');
                        }
                        //}
                    }
                }
            }
        }
    }
}